//
//  FriendsView.swift
//  ShouChong
//
//  Created by yw on 2024/9/23.
//

import SwiftUI

struct FriendsView: View {
    @ObservedObject var vm:UserViewModel = UserViewModel()
    @State var showDetailView:Bool = false
    @State var list:[UserModel] = []
    @State var currentModel:UserModel?
    var body: some View {
        ScrollView(showsIndicators: false, content: {
            VStack(content: {
                ForEach(list){ item in
                    HStack {
                        Image(uiImage: item.image.imageFromBase64 ?? UIImage())
                            .resizable()
                            .frame(width: 48,height: 48)
                            .cornerRadius(40)
                        HStack(alignment: .top, content: {
                            Text(item.name)
                                .modifier(MainTextModifier(fontSize: 16, fontWeight: .medium, foreColor: Color("color_text_black")))
                            if item.vip == "1" {
                                Image("icon_vip")
                                    .resizable()
                                    .frame(width: 16,height: 16)
                            }
                        })
                        Spacer()
                        Image(systemName: "chevron.right")
                            .foregroundColor(Color("color_select"))
                            .padding(.trailing,16)
                            .padding(.leading,4)
                            
                    }
                    .padding(.horizontal,10)
                    .padding(.vertical,10)
                    .background(Color("color_white"))
                    
                    .cornerRadius(20)
                    .padding(.horizontal,20)
                    .onTapGesture {
                        self.currentModel = item
                       goToDetail()
                    }
                    
                }
            })
           
        })
        .overlay(alignment: .bottom, content: {
            if showDetailView {
                FriendDetailView(showDetailView: $showDetailView,currentModel: currentModel!)
//                    .frame(width: kScreenWidth, height: kScreenHeight * 0.7)
//                    .transition(AnyTransition(.move(edge: .bottom)))
//                    .animation(.easeInOut)
            }
        })
        .edgesIgnoringSafeArea(.bottom)
        .onAppear{
                vm.getActiveUsers { models in
                    DPrint(models)
                    self.list = models
                }
            }
    }
    
    
    private func goToDetail() {
        let generator = UIImpactFeedbackGenerator(style: .light)
        generator.prepare()
        generator.impactOccurred()
        withAnimation(.spring()) {
            showDetailView.toggle()
        }
    }
    
    
}


